from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from eatfun_backend import settings

def upload_file(file_object,key,bucket='eatfun01-1303030821'):
    # 设置用户属性, 包括secret_id, secret_key, region
    # appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成
    secret_id = settings.TENCENT_SECRET_ID # 替换为用户的secret_id
    secret_key = settings.TENCENT_SECRET_KEY # 替换为用户的secret_key
    region = settings.TENCENT_CITY  # 替换为用户的region
    token = None  # 使用临时密钥需要传入Token，默认为空,可不填
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
    client = CosS3Client(config)

    response = client.upload_file_from_buffer(
        Bucket=bucket,
        # LocalFilePath=file_object,
        Body=file_object,
        Key=key,
        # PartSize=10,
        # MAXThread=10,
    )
    return "https://{0}.cos.{1}.myqcloud.com/{2}".format(bucket, region, key)
